﻿using FinalTest_Algonrithm;
using HYBurnBase;
using HYEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static FinalTest_Algonrithm.HuayaLibCalib;

namespace StationDemo
{
    public class ReadMapFunction
    {

        public static cv_pinhole_3k_intrin Function_3K模型赋值(BurnRead ReadMap)
        {
            cv_pinhole_3k_intrin cv_Pinhole_3K_Intrin = new cv_pinhole_3k_intrin();
            cv_Pinhole_3K_Intrin.k = new double[3];
            cv_Pinhole_3K_Intrin.p = new double[2];

            foreach (BurnReadItem item in ReadMap.BurnItems)
            {

                if (item.Content == BurnContent.EFLX) cv_Pinhole_3K_Intrin.fx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.EFLY) cv_Pinhole_3K_Intrin.fy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODX) cv_Pinhole_3K_Intrin.cx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODY) cv_Pinhole_3K_Intrin.cy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K1) cv_Pinhole_3K_Intrin.k[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K2) cv_Pinhole_3K_Intrin.k[1] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K3) cv_Pinhole_3K_Intrin.k[2] = item.BurnData.ToDouble();

                if (item.Content == BurnContent.P1) cv_Pinhole_3K_Intrin.p[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.P2) cv_Pinhole_3K_Intrin.p[1] = item.BurnData.ToDouble();
            }

            return cv_Pinhole_3K_Intrin;

        }

        public static cv_pinhole_6k_intrin Function_6K模型赋值(BurnRead ReadMap)
        {
            cv_pinhole_6k_intrin cv_Pinhole_6K_Intrin = new cv_pinhole_6k_intrin();
            cv_Pinhole_6K_Intrin.k = new double[6];
            cv_Pinhole_6K_Intrin.p = new double[2];

            foreach (BurnReadItem item in ReadMap.BurnItems)
            {

                if (item.Content == BurnContent.EFLX) cv_Pinhole_6K_Intrin.fx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.EFLY) cv_Pinhole_6K_Intrin.fy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODX) cv_Pinhole_6K_Intrin.cx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODY) cv_Pinhole_6K_Intrin.cy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K1) cv_Pinhole_6K_Intrin.k[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K2) cv_Pinhole_6K_Intrin.k[1] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K3) cv_Pinhole_6K_Intrin.k[2] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K4) cv_Pinhole_6K_Intrin.k[3] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K5) cv_Pinhole_6K_Intrin.k[4] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K6) cv_Pinhole_6K_Intrin.k[5] = item.BurnData.ToDouble();

                if (item.Content == BurnContent.P1) cv_Pinhole_6K_Intrin.p[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.P2) cv_Pinhole_6K_Intrin.p[1] = item.BurnData.ToDouble();
            }
            return cv_Pinhole_6K_Intrin;

        }

        public static cv_fisheye_intrin Function_鱼眼模型赋值(BurnRead ReadMap)
        {
            cv_fisheye_intrin cv_fisheye_intrin = new cv_fisheye_intrin();
            cv_fisheye_intrin.k = new double[4];


            foreach (BurnReadItem item in ReadMap.BurnItems)
            {

                if (item.Content == BurnContent.EFLX) cv_fisheye_intrin.fx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.EFLY) cv_fisheye_intrin.fy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODX) cv_fisheye_intrin.cx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODY) cv_fisheye_intrin.cy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K1) cv_fisheye_intrin.k[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K2) cv_fisheye_intrin.k[1] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K3) cv_fisheye_intrin.k[2] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K4) cv_fisheye_intrin.k[3] = item.BurnData.ToDouble();


            }
            return cv_fisheye_intrin;

        }
        public static cv_mei_intrin Function_MEI模型赋值(BurnRead ReadMap)
        {
            cv_mei_intrin cv_mei_intrin = new cv_mei_intrin();
            cv_mei_intrin.k = new double[2];
            cv_mei_intrin.p = new double[2];

            foreach (BurnReadItem item in ReadMap.BurnItems)
            {

                if (item.Content == BurnContent.EFLX) cv_mei_intrin.fx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.EFLY) cv_mei_intrin.fy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODX) cv_mei_intrin.cx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODY) cv_mei_intrin.cy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K1) cv_mei_intrin.k[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K2) cv_mei_intrin.k[1] = item.BurnData.ToDouble();

                if (item.Content == BurnContent.P1) cv_mei_intrin.p[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.P2) cv_mei_intrin.p[1] = item.BurnData.ToDouble();

                if (item.Content == BurnContent.MEI) cv_mei_intrin.meiXi = item.BurnData.ToDouble();

            }
            return cv_mei_intrin;

        }
        public static division_intrin Function_除法模型赋值(BurnRead ReadMap)
        {
            division_intrin division_intrin = new division_intrin();
            division_intrin.k = new double[6];

            foreach (BurnReadItem item in ReadMap.BurnItems)
            {

                if (item.Content == BurnContent.EFLX) division_intrin.fx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.EFLY) division_intrin.fy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODX) division_intrin.cx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODY) division_intrin.cy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K1) division_intrin.k[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K2) division_intrin.k[1] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K3) division_intrin.k[2] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K4) division_intrin.k[3] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K5) division_intrin.k[4] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.K6) division_intrin.k[5] = item.BurnData.ToDouble();


            }
            return division_intrin;

        }
        public static ocam_intrin Function_OCAM模型赋值(BurnRead ReadMap)
        {
            ocam_intrin ocam_intrin = new ocam_intrin();
            ocam_intrin.affineParas = new double[5];
            ocam_intrin.polyCoeffs = new double[5];
            ocam_intrin.invPoly = new double[20];

            foreach (BurnReadItem item in ReadMap.BurnItems)
            {

                if (item.Content == BurnContent.CODX) ocam_intrin.cx = item.BurnData.ToDouble();
                if (item.Content == BurnContent.CODY) ocam_intrin.cy = item.BurnData.ToDouble();
                if (item.Content == BurnContent.AffineParas1) ocam_intrin.affineParas[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.AffineParas2) ocam_intrin.affineParas[1] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.AffineParas3) ocam_intrin.affineParas[2] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.AffineParas4) ocam_intrin.affineParas[3] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.AffineParas5) ocam_intrin.affineParas[4] = item.BurnData.ToDouble();

                if (item.Content == BurnContent.PolyCoeffs1) ocam_intrin.polyCoeffs[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.PolyCoeffs2) ocam_intrin.polyCoeffs[1] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.PolyCoeffs3) ocam_intrin.polyCoeffs[2] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.PolyCoeffs4) ocam_intrin.polyCoeffs[3] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.PolyCoeffs5) ocam_intrin.polyCoeffs[4] = item.BurnData.ToDouble();


                if (item.Content == BurnContent.InvPoly1) ocam_intrin.invPoly[0] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly2) ocam_intrin.invPoly[1] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly3) ocam_intrin.invPoly[2] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly4) ocam_intrin.invPoly[3] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly5) ocam_intrin.invPoly[4] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly6) ocam_intrin.invPoly[5] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly7) ocam_intrin.invPoly[6] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly8) ocam_intrin.invPoly[7] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly9) ocam_intrin.invPoly[8] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly10) ocam_intrin.invPoly[9] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly11) ocam_intrin.invPoly[10] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly12) ocam_intrin.invPoly[11] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly13) ocam_intrin.invPoly[12] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly14) ocam_intrin.invPoly[13] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly15) ocam_intrin.invPoly[14] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly16) ocam_intrin.invPoly[15] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly17) ocam_intrin.invPoly[16] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly18) ocam_intrin.invPoly[17] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly19) ocam_intrin.invPoly[18] = item.BurnData.ToDouble();
                if (item.Content == BurnContent.InvPoly20) ocam_intrin.invPoly[19] = item.BurnData.ToDouble();

            }
            return ocam_intrin;

        }




        //读取标定数据
        public static bool NormalBurn(int HardId, ref BurnRead Read)
        {
            #region 读数据
            //获取读保护地址 1.打开读保护
            if (Read.BurnInfo.b_打开烧录保护)
            {
                foreach (BurnDefendInfo info in Read.BurnDefendInfos)
                {
                    if (info.烧录保护类型 == DefendBurnType.打开读取保护)
                    {
                        //WriteI2C(int nDeviceIndex, int slave, int addr, int value, int mode)
                        if (!ModuleCaptureMgr.Instance.WriteI2C(HardId, info.SlaveID.ToByte(), info.烧录保护地址.ToInt(), info.烧录保护数据.ToInt(), info.I2CMode.ToInt()))
                        {
                            // Form_Auto.EventShowLog(Name, $"打开读取保护写入失败，烧录保护地址：{info.烧录保护地址}, 烧录保护数据：{info.烧录保护数据}", LogType.Err);
                            MessageBox.Show($"打开读取保护写入失败，烧录保护地址：{info.烧录保护地址}, 烧录保护数据：{info.烧录保护数据}");
                            return false;
                        }
                    }
                }
            }


            //2.读出数据
            foreach (BurnReadItem item in Read.BurnItems)
            {
                List<byte> Reads = new List<byte>();
                for (int i = 0; i < item.BurnLength; i++)
                {
                    bool ReadResult = false;
                    int timeOut = 3;
                    int byRead = 0;
                ReRead:
                    ReadResult = ModuleCaptureMgr.Instance.ReadI2C(HardId, Read.BurnInfo.i_I2CRead地址.StringOXToByteAdd(), item.FirstAddress.StringOXToIntAdd() + i, ref byRead, Read.BurnInfo.i_I2C模型);
                    if (!ReadResult && --timeOut > 0)
                    {
                        Thread.Sleep(50);
                        goto ReRead;
                    }
                    else if (!ReadResult)
                    {
                        // Form_Auto.EventShowLog(Name, $"读取烧录数据失败，读取地址：{item.FirstAddress.StringOXToIntAdd() + i}，读取数据：{byRead}", LogType.Err);
                        MessageBox.Show($"读取烧录数据失败，读取地址：{item.FirstAddress.StringOXToIntAdd() + i}，读取数据：{byRead}");
                        return false;
                    }
                    Reads.Add((byte)byRead);
                }
                item.BurnData_Hex = Reads.ToArray().ByteToHex();
                item.BurnData = item.ByteToData();





                //if (item.BurnData_Hex != Reads.ToArray().ByteToHex())
                //{
                //   // Form_Auto.EventShowLog(Name, $"读取烧录数据比对失败，烧录内容为：{item.Content}，首地址为：{item.FirstAddress}，读出数据为：{Reads.ToArray().ByteToHex()}，写入数据为：{item.BurnData_Hex}", LogType.Err);
                //    MessageBox.Show($"读取烧录数据比对失败，烧录内容为：{item.Content}，首地址为：{item.FirstAddress}，读出数据为：{Reads.ToArray().ByteToHex()}，写入数据为：{item.BurnData_Hex}");
                //    return false;
                //}
            }
            //3.关闭读保护
            if (Read.BurnInfo.b_打开烧录保护)
            {
                foreach (BurnDefendInfo info in Read.BurnDefendInfos)
                {
                    if (info.烧录保护类型 == DefendBurnType.关闭读取保护)
                    {
                        if (!ModuleCaptureMgr.Instance.WriteI2C(HardId, info.SlaveID.ToByte(), info.烧录保护地址.ToInt(), info.烧录保护数据.ToInt(), info.I2CMode.ToInt()))
                        {
                            // Form_Auto.EventShowLog(Name, $"关闭读取保护写入失败，烧录保护地址：{info.烧录保护地址}, 烧录保护数据：{info.烧录保护数据}", LogType.Err);
                            MessageBox.Show($"关闭读取保护写入失败，烧录保护地址：{info.烧录保护地址}, 烧录保护数据：{info.烧录保护数据}");
                            return false;
                        }
                    }
                }
            }

            #endregion
            return true;
        }



    }
}
